#!/bin/bash

show_help() {
    cat<<USE
    USAGE:
      fbplay [-t audio-track] <filename or url>
    OPTIONS:
      --track|-t: audio track number
      --subs|-s: subtitle track number
USE
}

error() {
    printf 'ERROR: %s\n' "$@" >&2
}

clear_screen() {
    # Clear screen, hide cursor, foreground color black
    tput clear
    tput civis
    tput setaf 0
    if [ -n "$TMUX" ]; then
        tmux set -g status off
    fi
}

loading_screen() {
    clear_screen
    tput sgr0
    printf "%s\n%s\n" "$(figlet Loading...)" "$(fortune)" | cowsay -n -f $(ls /usr/share/cowsay/cows/ | shuf -n1)
    tput setaf 0
}

reset_screen() {
    # Clear screen, show cursor, foreground color reset
    tput cnorm
    tput sgr0
    tput clear
    if [ -n "$TMUX" ]; then
        tmux set -g status on
    fi
}

main() {
    if (( $# < 1 )); then
        show_help
        exit
    fi

    local audio_track=1
    local subtitle_track=1
    local media

    while :; do
        case $1 in
            -h|-\?|--help)
                show_help
                exit
                ;;
            -s|--subs)
                if [[ -n "$2" ]]; then
                    subtitle_track=$2
                    shift 2
                    continue
                else
                    error '"-s|--subs" requires a non-empty option argument.'
                    exit 1
                fi
                ;;
            --subs=?*)
                # Delete everything up to "=" and assign the remainder.
                subtitle_track=${1#*=}
                ;;
            --subs=)
                error '"--subs" requires a non-empty option argument.'
                exit 1
                ;;
            -t|--track)
                if [[ -n "$2" ]]; then
                    audio_track=$2
                    shift 2
                    continue
                else
                    error '"--track" requires a non-empty option argument.'
                    exit 1
                fi
                ;;
            --track=?*)
                # Delete everything up to "=" and assign the remainder.
                audio_track=${1#*=}
                ;;
            --track=)
                error '"--track" requires a non-empty option argument.'
                exit 1
                ;;
            --)
                # End of all options.
                shift
                break
                ;;
            -?*)
                ;;
            *)
                break
        esac

        shift
    done

    if [[ -z "$1" ]]; then
        error "A url or a file must be supplied"
        exit 1
    fi

    media="$1"
    loading_screen
    if [[ ! -f "$media" ]]; then
       if [[ "$media" == https://live* ]]; then
           media="$(youtube-dl --format hls-3628-0 --get-url "${media}" 2>/dev/null)"
       elif [[ "$media" == http* ]]; then
           media="$(youtube-dl --format 18 --get-url "${media}" 2>/dev/null)"
       else
           media="$(youtube-dl --format 18 --get-url "ytsearch:${media}" 2>/dev/null)"
       fi
    fi
    echo "$media" > /tmp/media
    clear_screen
    # Use global dbus control
    OMXPLAYER_DBUS_ADDR="/tmp/omxplayerdbus.global" omxplayer -t"$subtitle_track" -n"$audio_track" -z "$media" | echo ""
    reset_screen
}

main "$@"
